import {
  // useLocation,
  useNavigate,
  useParams,
  useSearchParams,
} from 'react-router-dom'

export function withRouter(WrapperComponent) {
  return (props) => {
    // 导航
    const navigate = useNavigate()
    // 动态路由参数 /detail/:id
    const params = useParams()
    // 查询字符串参数 /user?name="why"&age=18
    // const location = useLocation()
    const [searchParams] = useSearchParams()
    // 将URLSearchParams转换为普通对象
    const query = Object.fromEntries(searchParams)

    const router = {
      navigate,
      params,
      // location,
      query,
    }

    return <WrapperComponent {...props} router={router} />
  }
}
